// components/vcode/vcode.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isQrcode: {
      type: Boolean,
      value: false
    },
    title: {
      type: String,
      value: '验证手机'
    }
  },
  attached() {
    //检测是否有手机号码
    app.login().then(() => {
      let {
        mobile = ''
      } = app.$getUser();
      if (mobile) {
        this.setData({
          userMobile: mobile
        });
        this.updateUserMobile(mobile);
      }
      this.setData({
        isMandatoryPhone: xhs.getStorageSync('isMandatoryPhone')
      });
    });
  },
  data: {
    isMandatoryPhone: '0',
    //是否扫码强制授权手机
    hasValidate: false,
    title: '获取验证码',
    countTime: 60,
    timer: '',
    loading: '',
    toggle: false,
    userMobile: '',
    inputMobile: '',
    validateMobile: '',
    vcode: ''
  },
  /**
   * 组件的方法列表
   */
  methods: {
    refresh() {
      app.login().then(() => {
        let {
          mobile = ''
        } = app.$getUser();
        if (mobile) {
          this.setData({
            userMobile: mobile
          });
          this.updateUserMobile(mobile);
        }
        this.setData({
          isMandatoryPhone: xhs.getStorageSync('isMandatoryPhone')
        });
      });
    },
    setMobile(e) {
      let {
        value
      } = e.detail;
      this.setData({
        inputMobile: value
      });
    },
    setVcode(e) {
      let {
        value
      } = e.detail;
      this.setData({
        vcode: value
      });
    },
    closeDialog() {
      this.setData({
        toggle: false,
        vcode: ''
      });
    },
    submit() {
      let {
          inputMobile,
          vcode
        } = this.data,
        param = {
          cellphone: inputMobile,
          vcode
        };
      if (!/^\d{6}$/.test(vcode)) {
        return xhs.showModal({
          title: '提示',
          showCancel: false,
          content: '请输入验证码'
        });
      }
      app.$postData('/user/user/verify-cellphone', param).then(data => {
        if (data.errCode == 0) {
          this.updateUserMobile(param.cellphone);
          this.closeDialog();
        } else {
          xhs.showToast({
            title: data.errInfo,
            icon: 'none',
            mask: true
          });
        }
      });
    },
    sqFn() {
      this.setData({
        isSqdl: true
      });
    },
    okphone(e) {
      this.getMobile(e.detail);
    },
    getMobile(e) {
      let {
        iv,
        encryptedData,
        errMsg
      } = e.detail;
      if (errMsg === 'getPhoneNumber:ok') {
        app.$getData('/user/info/get-phone-number', {
          iv,
          encryptedData
        }).then(data => {
          if (data.errCode == 0) {
            let {
              phoneNumber
            } = data.data;
            this.updateUserMobile(phoneNumber);
          }
        });
      } else {
        this.setData({
          toggle: true
        });
      }
    },
    updateUserMobile(validateMobile) {
      this.setData({
        validateMobile,
        hasValidate: true
      });
      this.triggerEvent('getMobile', {
        mobile: validateMobile
      });
      this.triggerEvent('checkMobile', true);
      if (!this.data.userMobile) {
        let user = app.$getUser();
        user.mobile = validateMobile;
        xhs.setStorage({
          key: 'user',
          data: user
        });
      }
    }
  }
});